using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using Services.Menu;

namespace BowMaster.Menu
{
    public enum InputType
    {
        SingleDrag,
        PinchToFire
    }
    class OptionsMenu : DrawableGameComponent
    {


        InputManager input_manager;
        Texture2D buttonbg, screen_bg;
        SpriteFont font, font_t, font_p;
        MyButton sound_text, sound_b, type_text, type_b;
        MyButton title, back;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        protected float press_wait;
        protected readonly float PRESS_INTERVAL = 0.5f;
        Texture2D drag_t, pinch_t, double_tap_t;
        Rectangle rect1, rect2;
        MyButton text1, text2;
        public OptionsMenu(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }
        protected override void LoadContent()
        {
            //buttonbg = Game.Content.Load<Texture2D>("Levels/whiteTexture");
            buttonbg = Game.Content.Load<Texture2D>("Textures/emptyPixel");
            font = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
            font_p = Game.Content.Load<SpriteFont>("Fonts/Arial26Ptx");
            font_t = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
            var font_o = Game.Content.Load<SpriteFont>("Fonts/Arial16Ptx");
            screen_bg = Game.Content.Load<Texture2D>("Textures/LevelBackground");
            var whiteBox = Game.Content.Load<Texture2D>("Textures/bar_box");
            Color textColor = new Color (8,141,217);
            pinch_t = Game.Content.Load<Texture2D>("Textures/pinch");
            drag_t = Game.Content.Load<Texture2D>("Textures/drag");
            double_tap_t = Game.Content.Load<Texture2D>("Textures/double_tap");
            sound_text = new MyButton(new Rectangle(40, 200, 120, 100), Strings.Menu_Sound, textColor, font_p, TextPos.Left);
            sound_b = new MyButton(new Rectangle(200, 200, 280, 100), StateManager.OptionsState.sound ? Strings.On : Strings.Off, textColor, font_p);
            type_text = new MyButton(new Rectangle(40, 300, 120, 100), Strings.Menu_InputType, textColor, font_p, TextPos.Left);
            type_b = new MyButton(new Rectangle(200, 300, 280, 100), getInputTypeString(), textColor, font_p, TextPos.Center);

            //reset = new MyButton(new Rectangle(0, 380, 480, 80), Strings.Menu_ResetGame, textColor, font, bgColor, buttonbg);
            //credits = new MyButton(new Rectangle(0, 460, 480, 80), Strings.Menu_Credits, textColor, font, bgColor, buttonbg);
            //exit = new MyButton(new Rectangle(0, 540, 480, 80), Strings.Menu_Exit, textColor, font, bgColor, buttonbg);

            title = new MyButton(new Rectangle(20, 0, 440, 200), Strings.Menu_Options, textColor, font_t);
            back = new MyButton(new Rectangle(0, 700, 480, 100), Strings.Menu_Back, textColor, font);

            rect1 = new Rectangle(20, 400, 150, 150);
            rect2 = new Rectangle(20, 575, 150, 150);

            text1 = new MyButton(new Rectangle(200, 400, 480 - 260, 150), "", textColor, font_o, TextPos.Left);
            text2 = new MyButton(new Rectangle(200, 575, 480 - 260, 150), "", textColor, font_o, TextPos.Left);
            UpdateButtons();

            base.LoadContent();
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            base.Dispose(disposing);
        }
        public override void Update(GameTime gameTime)
        {
            if (press_wait < PRESS_INTERVAL)
                press_wait += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Storage.SaveOptionsState(StateManager.OptionsState);
                StateManager.SelectMainMenu();
            } 
            base.Update(gameTime);
        }

        Rectangle bg_rect = new Rectangle(0, 0, 480, 800);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            
            spriteBatch.Begin();

            spriteBatch.Draw(screen_bg, bg_rect, Color.White);

            title.Draw(spriteBatch);
            
            sound_b.Draw(spriteBatch);
            sound_text.Draw(spriteBatch);
            type_b.Draw(spriteBatch);
            type_text.Draw(spriteBatch);
            back.Draw(spriteBatch);

            spriteBatch.Draw(drag_t, rect1, Color.White);
            spriteBatch.Draw(pinch_t, rect2, Color.White);
            text1.Draw(spriteBatch);
            text2.Draw(spriteBatch);
            
            spriteBatch.End();
            base.Draw(gameTime);
        }
        Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            tp.X = (int)tap_position.X;
            tp.Y = (int)tap_position.Y;

            if (back.tapped_on(tp))
            {
                Storage.SaveOptionsState(StateManager.OptionsState);
                StateManager.SelectMainMenu();
            }
            else if (sound_b.tapped_on(tp))
            {
                StateManager.OptionsState.sound = !StateManager.OptionsState.sound;
                if (StateManager.OptionsState.sound)
                    sound_b.text = Strings.On;
                else
                    sound_b.text = Strings.Off;
            }
            else if (type_b.tapped_on(tp))
            {
                StateManager.OptionsState.inputType = getNextInputType();
                type_b.text = getInputTypeString();
                UpdateButtons();
            }

            
        }
        private String getInputTypeString()
        {
            switch (StateManager.OptionsState.inputType)
            {
                case InputType.SingleDrag:
                    return Strings.Menu_SingleDrag;

                case InputType.PinchToFire:
                    return Strings.Menu_PinchToFire;
            }
            return "";
        }
        private InputType getNextInputType()
        {
            switch (StateManager.OptionsState.inputType)
            {
                case InputType.SingleDrag:
                    return InputType.PinchToFire;

                case InputType.PinchToFire:
                    return InputType.SingleDrag;
            }
            return InputType.SingleDrag;
        }
        private void UpdateButtons()
        {
            switch (StateManager.OptionsState.inputType)
            {
                case InputType.PinchToFire:
                    text1.text = Strings.Drag + "\n\n" + Strings.Drag2_1;
                    text2.text = Strings.Pinch + "\n\n" + Strings.Pinch2_1;
                    break;
                case InputType.SingleDrag:
                    text1.text = Strings.Drag + "\n\n" + Strings.Drag1_1 + "\n" + Strings.Drag1_2 + "\n" + Strings.Drag1_3 + "\n" + Strings.Drag1_4;
                    text2.text = Strings.Pinch + "\n\n" + Strings.Pinch1;
                    break;
            }
            text1.UpdateTextPos();
            text2.UpdateTextPos();
        }
    }
}
